package source;

/*
 * Author: Aqeel Kazmi
 * Email: aqeelhkazmi@gmail.com
*/

import javax.media.*;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;

public class MediaMenu implements ActionListener,ItemListener
{		
	KeyListen ml = null;
	MediaPlayerFrame mpf = null;

	GraphicsEnvironment ge = null;
	GraphicsDevice gd = null;
	DisplayMode dm = null;
	
public MediaMenu(MediaPlayerFrame mpf)
{	
		
	try
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

	}
	catch(Exception e)
	{
		System.out.println(e);
	}
		this.mpf = mpf;
		MenuBar m = new MenuBar();
        
		Menu file = new Menu("File");
        MenuItem open = new MenuItem("Open File");
        open.addActionListener(this);
        file.add(open);
        
        MenuItem openNetwork = new MenuItem("Open Remote File");
        openNetwork.addActionListener(this);
        file.add(openNetwork);
        m.add(file);
        
        file.addSeparator();
        
        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(this);
        file.add(exit);
        m.add(file);

        Menu view = new Menu("View");
        MenuItem fscreen = new MenuItem("Full Screen");
        fscreen.addActionListener(this);
        view.add(fscreen);
    	MenuItem playlist = new MenuItem("Play List");
        playlist.addActionListener(this);
        view.add(playlist);  
        m.add(view);
	
        
        ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gd = ge.getDefaultScreenDevice();
        dm = gd.getDisplayMode();  
        mpf.fullScreenFrame = new Frame();
        mpf.fullScreenFrame.setLayout(new BorderLayout());
        mpf.fullScreenFrame.setSize(dm.getWidth(),dm.getHeight());   		
        mpf.fullScreenFrame.setUndecorated(true);
        mpf.fullScreenFrame.setFocusable(true);

        Menu options = new Menu("Play");
        CheckboxMenuItem loop = new CheckboxMenuItem("Loop");
        loop.addItemListener(this);
        options.add(loop);
        m.add(options);
        
		Menu help = new Menu("Help");
        MenuItem about = new MenuItem("About");
        about.addActionListener(this);
        help.add(about);
        m.add(help);
           
        ml = new KeyListen(mpf,ml); 
	
        mpf.fullScreenFrame.addKeyListener(new KeysListen(mpf));
        mpf.backGround.addKeyListener(new KeysListen(mpf));        
        
        
        mpf.setMenuBar(m);
}

public void actionPerformed(ActionEvent ae)
    {	
    	String check = ae.getActionCommand();
    	if(check.equals("Open File"))
    	{
    		try{
		String ss = mpf.GetFile();
		MediaLocator ml = new MediaLocator((new File(ss)).toURL());
		mpf.setLocator(ml);
    	}catch (Exception e){
    		System.out.println(e);
    	}
    	}
    	
    	if(check.equals("Exit"))
    	{
    	mpf.closeProcessEvent();
    	}  
    	
    	if(check.equals("Open Remote File"))
    		{
    	String name = JOptionPane.showInputDialog(mpf,"Enter Server IP and Port from which to play files as [xxx.xx.xx.xx:xxxx]");
    	//if(name!=null){
    	//Pattern p = Pattern.compile("[1][7|9][2][.][1][6][.][0-9][0-9][.][0-9]*[0-9][:][0-9]+");
    	//Matcher m = p.matcher(name);
    	//if(m.matches()==false){
    		//JOptionPane.showMessageDialog(mpf,"Information provided is not in correct pattern ");
    	  	//		}else{
    	  				String ipPort[] = name.split(":");
    	  				String ip = ipPort[0];
    	  				int port =Integer.parseInt(ipPort[1]);
    	  				mpf.rf.sendRequest(ip,port);
    	  				}
    		//}
    	//}
    	

    	if(check.equals("Full Screen"))
	
    	{
    		if(mpf.fullScreen==false)
    			return;
    		if(mpf.getPlayer().getVisualComponent()!=null)
    		{
			mpf.fullScreenFrame.removeAll();
			mpf.fullScreenFrame.setFocusable(true);
			mpf.setVisible(false);
    		mpf.fullScreenFrame.add(mpf.getPlayer().getVisualComponent(),BorderLayout.CENTER);    				
			mpf.fullScreenFrame.setVisible(true);
		}
			else
		{					
    		mpf.fullScreenFrame.removeAll();
			mpf.fullScreenFrame.setFocusable(true);
			mpf.setVisible(false);
    		mpf.fullScreenFrame.add(mpf.backGround,BorderLayout.CENTER);    				
			mpf.fullScreenFrame.setVisible(true);	
		}		    		
    	
	}
    	if(check.equals("Play List"))	
    	{
    		mpf.sf.showList();
    	}
    	if(check.equals("About"))	
    	{
    		JOptionPane.showMessageDialog(mpf,"\nOpen Source Software by A.H KAZMI (Gift University , School of Computer\nScience) ,School of Computer Science grants you (Licensee) non-exclusive,\nroyalty free, license to use, modify and redistribute this software in source\nand binary code form, This software is provided AS it is, without a warranty\nof any kind.");
    	}
    }
    
    public void itemStateChanged(ItemEvent ie)
    {       	
    	if(ie.getStateChange()==ie.SELECTED)
    	{    	
    		mpf.loops = true;    		
    	}
    	
    	if(ie.getStateChange()==ie.DESELECTED)
    	{    		
    		mpf.loops = false;    		    		
    	}    	
    }

}

class KeyListen extends KeyAdapter
{
	MediaPlayerFrame mpf = null;
	KeyListen ml = null;
	
	public KeyListen(MediaPlayerFrame mpf,KeyListen ml)
	{		
		this.ml = ml;
		this.mpf = mpf;
	}
	
	public void keyPressed(KeyEvent ke)
        	{		
			int key = ke.getKeyCode();

			if(key==ke.VK_ESCAPE)
			{
				if(mpf.fullScreen==false)
					return;
					
					
					if(mpf.fullScreenFrame.isVisible()==false)
					{
						if(mpf.getPlayer().getVisualComponent()!=null)
					{
							mpf.fullScreenFrame.removeAll();
							mpf.fullScreenFrame.setFocusable(true);
							mpf.setVisible(false);
							mpf.fullScreenFrame.add(mpf.getPlayer().getVisualComponent(),BorderLayout.CENTER);
							//mpf.fullScreenFrame.add(mpf.controlPanel.controllerBar.cpanel(),BorderLayout.SOUTH);
							mpf.fullScreenFrame.setVisible(true);
					}
						else 
					{				   		
							mpf.fullScreenFrame.removeAll();
							mpf.fullScreenFrame.setFocusable(true);
							mpf.setVisible(false);
							mpf.fullScreenFrame.add(mpf.backGround,BorderLayout.CENTER);    									
							mpf.fullScreenFrame.setVisible(true);
					}
			    			
						}	

					
					else if(mpf.fullScreenFrame.isVisible()==true)
						{	
						if(mpf.getPlayer().getVisualComponent()!=null)        		
				            	mpf.displayAndControl.add(mpf.getPlayer().getVisualComponent(),BorderLayout.CENTER);		
						else
						mpf.displayAndControl.add(mpf.backGround,BorderLayout.CENTER);	
						mpf.setVisible(true);					
	        				mpf.fullScreenFrame.setVisible(false);        		       		
						}
			}
			
        		
			if(key==ke.VK_LEFT)
			{
				mpf.controlPanel.controllerBar.setValue((int)mpf.getPlayer().getMediaTime().getSeconds()-5);
			}

			if(key==ke.VK_RIGHT)
			{				
				mpf.controlPanel.controllerBar.setValue((int)mpf.getPlayer().getMediaTime().getSeconds()+5);	
			}     

			if(key==ke.VK_UP)
			{
				String v = mpf.getPlayer().getVolumeLevel();
				int vol = Integer.parseInt(v);
				if(vol<6)
				{	
				vol++;
				v = String.valueOf(vol);
				mpf.getPlayer().setVolumeLevel(v);						
				mpf.controlPanel.controllerBar.vb.setValue(vol);
				}
				
			}

			if(key==ke.VK_DOWN)
			{
				String v = mpf.getPlayer().getVolumeLevel();				
				int vol = Integer.parseInt(v);
				if(vol>0)
				{	
				vol--;
				v = String.valueOf(vol);
				mpf.getPlayer().setVolumeLevel(v);						
				mpf.controlPanel.controllerBar.vb.setValue(vol);						
				}				
			}

			if(key==ke.VK_SPACE)
			{
				if(mpf.getPlayer().getState()==mpf.getPlayer().Started)
				mpf.getPlayer().stop();
				else
				{
				if(mpf.getPlayer().getState()==mpf.getPlayer().Prefetched)
				mpf.getPlayer().start();
				}
			}
			
			if(key==ke.VK_S)
			{
				if(mpf.getPlayer().getState()==mpf.getPlayer().Started)
				{
				mpf.getPlayer().setMediaTime(new Time(0));
				mpf.getPlayer().stop();
				}				
			}	
			

   	}    				

}